# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=labelme
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=4.5.6
pkgrel=3
pkgdesc='Image Polygonal Annotation with Python (polygon, rectangle, circle, line, point and image-level flag annotation). (mingw-w64)'
arch=('any')
url="https://github.com/wkentaro/labelme"
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-python-imgviz" "${MINGW_PACKAGE_PREFIX}-python-termcolor" "${MINGW_PACKAGE_PREFIX}-python-qtpy")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-pyqt5: Qt backend")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("$_realname-${pkgver}.tar.gz::https://github.com/wkentaro/labelme/archive/v${pkgver}.tar.gz")
sha256sums=('187700ed0d436f23c946a9417b8f48119cf213e3d7b509b442aef13d53cfe82f')

prepare() {  
  cd "$srcdir"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  local _PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${_PREFIX_WIN}/bin/||g" -i "${pkgdir}${MINGW_PREFIX}/bin/labelme_draw_json-script.py"
  sed -s "s|${_PREFIX_WIN}/bin/||g" -i "${pkgdir}${MINGW_PREFIX}/bin/labelme_draw_label_png-script.py"
  sed -s "s|${_PREFIX_WIN}/bin/||g" -i "${pkgdir}${MINGW_PREFIX}/bin/labelme_json_to_dataset-script.py"
  sed -s "s|${_PREFIX_WIN}/bin/||g" -i "${pkgdir}${MINGW_PREFIX}/bin/labelme_on_docker-script.py"
  sed -s "s|${_PREFIX_WIN}/bin/||g" -i "${pkgdir}${MINGW_PREFIX}/bin/labelme-script.py"

  install -Dm644 labelme/icons/icon.png "${pkgdir}${MINGW_PREFIX}/share/pixmaps/labelme.png"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
